home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Tool Chest / Development Tools & Languages / • Other Platforms / PCCTS 1.31 / antlr / fset.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-10  |  25.9 KB  |  911 lines  |  [TEXT/MPS ]

  1. /*
  2.  * fset.c
  3.  *
  4.  * $Id: fset.c,v 1.14 1994/12/31 21:02:55 parrt Exp parrt $
  5.  * $Revision: 1.14 $
  6.  *
  7.  * Compute FIRST and FOLLOW sets.
  8.  *
  9.  * SOFTWARE RIGHTS
  10.  *
  11.  * We reserve no LEGAL rights to the Purdue Compiler Construction Tool
  12.  * Set (PCCTS) -- PCCTS is in the public domain.  An individual or
  13.  * company may do whatever they wish with source code distributed with
  14.  * PCCTS or the code generated by PCCTS, including the incorporation of
  15.  * PCCTS, or its output, into commerical software.
  16.  * 
  17.  * We encourage users to develop software with PCCTS.  However, we do ask
  18.  * that credit is given to us for developing PCCTS.  By "credit",
  19.  * we mean that if you incorporate our source code into one of your
  20.  * programs (commercial product, research project, or otherwise) that you
  21.  * acknowledge this fact somewhere in the documentation, research report,
  22.  * etc...  If you like PCCTS and have developed a nice tool with the
  23.  * output, please mention that you developed it using PCCTS.  In
  24.  * addition, we ask that this header remain intact in our source code.
  25.  * As long as these guidelines are kept, we expect to continue enhancing
  26.  * this system and expect to make other tools available as they are
  27.  * completed.
  28.  *
  29.  * ANTLR 1.31
  30.  * Terence Parr
  31.  * Parr Research Corporation
  32.  * with Purdue University and AHPCRC, University of Minnesota
  33.  * 1989-1995
  34.  */
  35. #include <stdio.h>
  36. #ifdef __cplusplus
  37. #ifndef __STDC__
  38. #define __STDC__
  39. #endif
  40. #endif
  41. #include "set.h"
  42. #include "syn.h"
  43. #include "hash.h"
  44. #include "generic.h"
  45. #include "dlgdef.h"
  46.  
  47. #ifdef __STDC__
  48. static void ensure_predicates_cover_ambiguous_lookahead_sequences(Junction *, Junction *, char *, Tree *);
  49. #else
  50. static void ensure_predicates_cover_ambiguous_lookahead_sequences();
  51. #endif
  52.  
  53. /*
  54.  * What tokens are k tokens away from junction q?
  55.  *
  56.  * Follow both p1 and p2 paths (unless RuleBlk) to collect the tokens k away from this
  57.  * node.
  58.  * We lock the junction according to k--the lookahead.  If we have been at this
  59.  * junction before looking for the same, k, number of lookahead tokens, we will
  60.  * do it again and again...until we blow up the stack.  Locks are only used on aLoopBlk,
  61.  * RuleBlk, aPlusBlk and EndRule junctions to remove/detect infinite recursion from
  62.  * FIRST and FOLLOW calcs.
  63.  *
  64.  * If p->jtype == EndRule we are going to attempt a FOLLOW.  (FOLLOWs are really defined
  65.  * in terms of FIRST's, however).  To proceed with the FOLLOW, p->halt cannot be
  66.  * set.  p->halt is set to indicate that a reference to the current rule is in progress
  67.  * and the FOLLOW is not desirable.
  68.  *
  69.  * If we attempt a FOLLOW and find that there is no FOLLOW or REACHing beyond the EndRule
  70.  * junction yields an empty set, replace the empty set with EOF.  No FOLLOW means that
  71.  * only EOF can follow the current rule.  This normally occurs only on the start symbol
  72.  * since all other rules are referenced by another rule somewhere.
  73.  *
  74.  * Normally, both p1 and p2 are followed.  However, checking p2 on a RuleBlk node is
  75.  * the same as checking the next rule which is clearly incorrect.
  76.  *
  77.  * Cycles in the FOLLOW sense are possible.  e.g. Fo(c) requires Fo(b) which requires
  78.  * Fo(c).  Both Fo(b) and Fo(c) are defined to be Fo(b) union Fo(c).  Let's say
  79.  * Fo(c) is attempted first.  It finds all of the FOLLOW symbols and then attempts
  80.  * to do Fo(b) which finds of its FOLLOW symbols.  So, we have:
  81.  *
  82.  *                  Fo(c)
  83.  *                 /     \
  84.  *              a set    Fo(b)
  85.  *                      /     \
  86.  *                   a set    Fo(c) .....Hmmmm..... Infinite recursion!
  87.  *
  88.  * The 2nd Fo(c) is not attempted and Fo(b) is left deficient, but Fo(c) is now
  89.  * correctly Fo(c) union Fo(b).  We wish to pick up where we left off, so the fact
  90.  * that Fo(b) terminated early means that we lack Fo(c) in the Fo(b) set already
  91.  * laying around.  SOOOOoooo, we track FOLLOW cycles.  All FOLLOW computations are
  92.  * cached in a hash table.  After the sequence of FOLLOWs finish, we reconcile all
  93.  * cycles --> correct all Fo(rule) sets in the cache.
  94.  *
  95.  * Confused? Good! Read my MS thesis [Purdue Technical Report TR90-30].
  96.  * TJP 8/93 -- can now read PhD thesis from Purdue.
  97.  *
  98.  * Also, FIRST sets are cached in the hash table.  Keys are (rulename,Fi/Fo,k).
  99.  * Only FIRST sets, for which the FOLLOW is not included, are stored.
  100.  *
  101.  * SPECIAL CASE of (...)+ blocks:
  102.  * I added an optional alt so that the alts could see what
  103.  * was behind the (...)+ block--thus using enough lookahead
  104.  * to branch out rather than just enough to distinguish
  105.  * between alts in the (...)+.  However, when the FIRST("(...)+") is
  106.  * is needed, must not use this last "optional" alt.  This routine
  107.  * turns off this path by setting a new 'ignore' flag for
  108.  * the alt and then resetting it afterwards.
  109.  */
  110. set
  111. #ifdef __STDC__
  112. rJunc( Junction *p, int k, set *rk )
  113. #else
  114. rJunc( p, k, rk )
  115. Junction *p;
  116. int k;
  117. set *rk;
  118. #endif
  119. {
  120.     set a, b;
  121.     require(p!=NULL,                "rJunc: NULL node");
  122.     require(p->ntype==nJunction,    "rJunc: not junction");
  123.  
  124. #ifdef DBG_LL1
  125.     if ( p->jtype == RuleBlk ) fprintf(stderr, "FIRST(%s,%d) \n",((Junction *)p)->rname,k);
  126.     else fprintf(stderr, "rJunc: %s in rule %s\n",
  127.             decodeJType[p->jtype], ((Junction *)p)->rname);
  128. #endif
  129.     /* if this is one of the added optional alts for (...)+ then return */
  130.     if ( p->ignore ) return empty;
  131.  
  132.     /* locks are valid for aLoopBlk,aPlusBlk,RuleBlk,EndRule junctions only */
  133.     if ( p->jtype==aLoopBlk || p->jtype==RuleBlk ||
  134.          p->jtype==aPlusBlk || p->jtype==EndRule ) 
  135.     {
  136.         require(p->lock!=NULL, "rJunc: lock array is NULL");
  137.         if ( p->lock[k] )
  138.         {
  139.             if ( p->jtype == EndRule )    /* FOLLOW cycle? */
  140.             {
  141. #ifdef DBG_LL1
  142.                 fprintf(stderr, "FOLLOW cycle to %s: panic!\n", p->rname);
  143. #endif
  144.                 RegisterCycle(p->rname, k);
  145.             }
  146.             return empty;
  147.         }
  148.         if ( p->jtype == RuleBlk && p->end->halt )    /* check for FIRST cache */
  149.         {
  150.             CacheEntry *q = (CacheEntry *) hash_get(Fcache, Fkey(p->rname,'i',k));
  151.             if ( q != NULL )
  152.             {
  153.                 set_orin(rk, q->rk);
  154.                 return set_dup( q->fset );
  155.             }
  156.         }
  157.         if ( p->jtype == EndRule )        /* FOLLOW set cached already? */
  158.         {
  159.             CacheEntry *q = (CacheEntry *) hash_get(Fcache, Fkey(p->rname,'o',k));
  160.             if ( q != NULL )
  161.             {
  162. #ifdef DBG_LL1
  163.                 fprintf(stderr, "cache for FOLLOW(%s,%d):", p->rname,k);
  164.                 s_fprT(stderr, q->fset);
  165.                 if ( q->incomplete ) fprintf(stderr, " (incomplete)");
  166.                 fprintf(stderr, "\n");
  167. #endif
  168.                 if ( !q->incomplete )
  169.                 {
  170.                     return set_dup( q->fset );
  171.                 }
  172.             }
  173.         }
  174.         p->lock[k] = TRUE;    /* This rule is busy */
  175.     }
  176.  
  177.     a = b = empty;
  178.  
  179.     if ( p->jtype == EndRule )
  180.     {
  181.         if ( p->halt )                                /* don't want FOLLOW here? */
  182.         {
  183.             p->lock[k] = FALSE;
  184.             set_orel(k, rk);                        /* indicate this k value needed */
  185.             return empty;
  186.         }
  187.         FoPush(p->rname, k);                        /* Attempting FOLLOW */
  188.         if ( p->p1 == NULL ) set_orel((TokenInd!=NULL?TokenInd[EofToken]:EofToken), &a);/* if no FOLLOW assume EOF */
  189. #ifdef DBG_LL1
  190.         fprintf(stderr, "-->FOLLOW(%s,%d)\n", p->rname,k);
  191. #endif
  192.     }
  193.  
  194.     if ( p->p1 != NULL ) REACH(p->p1, k, rk, a);
  195.     
  196.     /* C a c h e  R e s u l t s */
  197.     if ( p->jtype == RuleBlk && p->end->halt )        /* can save FIRST set? */
  198.     {
  199.         CacheEntry *q = newCacheEntry( Fkey(p->rname,'i',k) );
  200.         /*fprintf(stderr, "Caching %s FIRST %d\n", p->rname, k);*/
  201.         hash_add(Fcache, Fkey(p->rname,'i',k), (Entry *)q);
  202.         q->fset = set_dup( a );
  203.         q->rk = set_dup( *rk );
  204.     }
  205.  
  206.     if ( p->jtype == EndRule )                        /* just completed FOLLOW? */
  207.     {
  208.         /* Cache Follow set */
  209.         CacheEntry *q = (CacheEntry *) hash_get(Fcache, Fkey(p->rname,'o',k));
  210.         if ( q==NULL )
  211.         {
  212.             q = newCacheEntry( Fkey(p->rname,'o',k) );
  213.             hash_add(Fcache, Fkey(p->rname,'o',k), (Entry *)q);
  214.         }
  215.         /*fprintf(stderr, "Caching %s FOLLOW %d\n", p->rname, k);*/
  216.         if ( set_nil(a) && !q->incomplete )
  217.         {
  218.             /* Don't ever save a nil set as complete.
  219.              * Turn it into an eof set.
  220.              */
  221.             set_orel(EofToken, &a);
  222.         }
  223.         set_orin(&(q->fset), a);
  224.         FoPop( k );
  225.         if ( FoTOS[k] == NULL && Cycles[k] != NULL ) ResolveFoCycles(k);
  226. #ifdef DBG_LL1
  227.         fprintf(stderr, "saving FOLLOW(%s,%d):", p->rname, k);
  228.         s_fprT(stderr, q->fset);
  229.         if ( q->incomplete ) fprintf(stderr, " (incomplete)");
  230.         fprintf(stderr, "\n");
  231. #endif
  232.     }
  233.     
  234.     if ( p->jtype != RuleBlk && p->p2 != NULL ) REACH(p->p2, k, rk, b);
  235.     if ( p->jtype==aLoopBlk || p->jtype==RuleBlk ||
  236.          p->jtype==aPlusBlk || p->jtype==EndRule ) 
  237.         p->lock[k] = FALSE;                            /* unlock node */
  238.  
  239.     set_orin(&a, b);
  240.     set_free(b);
  241.     return a;
  242. }
  243.  
  244. set
  245. #ifdef __STDC__
  246. rRuleRef( RuleRefNode *p, int k, set *rk_out )
  247. #else
  248. rRuleRef( p, k, rk_out )
  249. RuleRefNode *p;
  250. int k;
  251. set *rk_out;
  252. #endif
  253. {
  254.     set rk;
  255.     Junction *r;
  256.     int k2;
  257.     set a, rk2, b;
  258.     int save_halt;
  259.     RuleEntry *q = (RuleEntry *) hash_get(Rname, p->text);
  260.     require(p!=NULL,            "rRuleRef: NULL node");
  261.     require(p->ntype==nRuleRef,    "rRuleRef: not rule ref");
  262.  
  263. #ifdef DBG_LL1
  264.     fprintf(stderr, "rRuleRef: %s\n", p->text);
  265. #endif
  266.     if ( q == NULL )
  267.     {
  268.         warnFL( eMsg1("rule %s not defined",p->text), FileStr[p->file], p->line );
  269.         REACH(p->next, k, rk_out, a);
  270.         return a;
  271.     }
  272.     rk2 = empty;
  273.     r = RulePtr[q->rulenum];
  274.     if ( r->lock[k] )
  275.     {
  276.         errNoFL( eMsg2("infinite left-recursion to rule %s from rule %s",
  277.                         r->rname, p->rname) );
  278.         return empty;
  279.     }
  280.     save_halt = r->end->halt;
  281.     r->end->halt = TRUE;        /* don't let reach fall off end of rule here */
  282.     rk = empty;
  283.     REACH(r, k, &rk, a);
  284.     r->end->halt = save_halt;
  285.     while ( !set_nil(rk) ) {
  286.         k2 = set_int(rk);
  287.         set_rm(k2, rk);
  288.         REACH(p->next, k2, &rk2, b);
  289.         set_orin(&a, b);
  290.         set_free(b);
  291.     }
  292.     set_free(rk);                /* this has no members, but free it's memory */
  293.     set_orin(rk_out, rk2);        /* remember what we couldn't do */
  294.     set_free(rk2);
  295.     return a;
  296. }
  297.  
  298. /*
  299.  * Return FIRST sub k ( token_node )
  300.  *
  301.  * TJP 10/11/93 modified this so that token nodes that are actually
  302.  * ranges (T1..T2) work.
  303.  */
  304. set
  305. #ifdef __STDC__
  306. rToken( TokNode *p, int k, set *rk )
  307. #else
  308. rToken( p, k, rk )
  309. TokNode *p;
  310. int k;
  311. set *rk;
  312. #endif
  313. {
  314.     set a;
  315.     require(p!=NULL,            "rToken: NULL node");
  316.     require(p->ntype==nToken,    "rToken: not token node");
  317.  
  318. #ifdef DBG_LL1
  319.     fprintf(stderr, "rToken: %s\n", (TokenString(p->token)!=NULL)?TokenString(p->token):
  320.                                     ExprString(p->token));
  321. #endif
  322.     if ( k-1 == 0 )
  323.     {
  324.         if ( !set_nil(p->tset) ) return set_dup(p->tset);
  325.         return set_of(p->token);
  326.     }
  327.     REACH(p->next, k-1, rk, a);
  328.     
  329.     return a;
  330. }
  331.  
  332. set
  333. #ifdef __STDC__
  334. rAction( ActionNode *p, int k, set *rk )
  335. #else
  336. rAction( p, k, rk )
  337. ActionNode *p;
  338. int k;
  339. set *rk;
  340. #endif
  341. {
  342.     set a;
  343.     require(p!=NULL,            "rJunc: NULL node");
  344.     require(p->ntype==nAction,    "rJunc: not action");
  345.     
  346.     REACH(p->next, k, rk, a);    /* ignore actions */
  347.     return a;
  348. }
  349.  
  350.                 /* A m b i g u i t y  R e s o l u t i o n */
  351.  
  352.  
  353. void
  354. #ifdef __STDC__
  355. dumpAmbigMsg( set *fset, FILE *f, int want_nls )
  356. #else
  357. dumpAmbigMsg( fset, f, want_nls )
  358. set *fset;
  359. FILE *f;
  360. int want_nls;
  361. #endif
  362. {
  363.     int i;
  364.  
  365.     if ( want_nls ) fprintf(f, "\n\t");
  366.     else fprintf(f, " ");
  367.     for (i=1; i<=CLL_k; i++)
  368.     {
  369.         if ( i>1 )
  370.         {
  371.             if ( !want_nls ) fprintf(f, ", ");
  372.         }
  373.         if ( set_deg(fset[i]) > 3 && elevel == 1 )
  374.         {
  375.             int e,m;
  376.             fprintf(f, "{");
  377.             for (m=1; m<=3; m++)
  378.             {
  379.                 e=set_int(fset[i]);
  380.                 fprintf(f, " %s", TerminalString(e));
  381.                 set_rm(e, fset[i]);
  382.             }
  383.             fprintf(f, " ... }");
  384.         }
  385.         else s_fprT(f, fset[i]);
  386.         if ( want_nls ) fprintf(f, "\n\t");
  387.     }
  388.     fprintf(f, "\n");
  389. }
  390.  
  391. /*
  392.  * If delta is the set of ambiguous lookahead sequences, then make sure that
  393.  * the predicate(s) for productions alt1,alt2 cover the sequences in delta.
  394.  *
  395.  * For example,
  396.  *    a : <<PRED1>>? (A B|A C)
  397.  *      | b
  398.  *    ;
  399.  *    b : <<PRED2>>? A B
  400.  *      | A C
  401.  *      ;
  402.  *
  403.  * This should give a warning that (A C) predicts both productions and alt2
  404.  * does not have a predicate in the production that generates (A C).
  405.  *
  406.  * The warning detection is simple.  Let delta = LOOK(alt1) intersection LOOK(alt2).
  407.  * Now, if ( delta set-difference context(predicates-for-alt1) != empty then
  408.  * alt1 does not "cover" all ambiguous sequences.
  409.  *
  410.  * If ambig is nonempty, then ambig in LL(k) sense -> use tree info; else use fset
  411.  * info.  Actually, sets are used only if k=1 for this grammar.
  412.  */
  413. static void
  414. #ifdef __STDC__
  415. ensure_predicates_cover_ambiguous_lookahead_sequences( Junction *alt1, Junction *alt2, char *sub, Tree *ambig )
  416. #else
  417. ensure_predicates_cover_ambiguous_lookahead_sequences( alt1, alt2, sub, ambig )
  418. Junction *alt1;
  419. Junction *alt2;
  420. char *sub;
  421. Tree *ambig;
  422. #endif
  423. {
  424.     if ( !ParseWithPredicates ) return;
  425.  
  426.     if ( ambig!=NULL )
  427.     {
  428.         Tree *non_covered = NULL;
  429.         if ( alt1->predicate!=NULL )
  430.             non_covered = tdif(ambig, alt1->predicate, alt1->fset, alt2->fset);
  431.         if ( (non_covered!=NULL || alt1->predicate==NULL) && WarningLevel>1 )
  432.         {
  433.             fprintf(stderr, ErrHdr, FileStr[alt1->file], alt1->line);
  434.             fprintf(stderr, " warning: alt %d %shas no predicate to resolve ambiguity",
  435.                             alt1->altnum, sub);
  436.             if ( alt2->predicate!=NULL && non_covered!=NULL )
  437.             {
  438.                 fprintf(stderr, " upon");
  439.                 preorder(non_covered);
  440.             }
  441.             fprintf(stderr, "\n");
  442.         }
  443.         Tfree(non_covered);
  444.         if ( alt2->predicate!=NULL )
  445.             non_covered = tdif(ambig, alt2->predicate, alt1->fset, alt2->fset);
  446.         if ( (non_covered!=NULL || alt2->predicate==NULL) && WarningLevel>1 )
  447.         {
  448.             fprintf(stderr, ErrHdr, FileStr[alt2->file], alt2->line);
  449.             fprintf(stderr, " warning: alt %d %shas no predicate to resolve ambiguity",
  450.                             alt2->altnum, sub);
  451.             if ( alt2->predicate!=NULL && non_covered!=NULL )
  452.             {
  453.                 fprintf(stderr, " upon");
  454.                 preorder(non_covered);
  455.             }
  456.             fprintf(stderr, "\n");
  457.         }
  458.         Tfree(non_covered);
  459.     }
  460.     else if ( !set_nil(alt1->fset[1]) )
  461.     {
  462.         set delta, non_covered;
  463.         delta = set_and(alt1->fset[1], alt2->fset[1]);
  464.         non_covered = set_dif(delta, covered_set(alt1->predicate));
  465.         if ( set_deg(non_covered)>0 && WarningLevel>1 )
  466.         {
  467.             fprintf(stderr, ErrHdr, FileStr[alt1->file], alt1->line);
  468.             fprintf(stderr, " warning: alt %d %shas no predicate to resolve ambiguity",
  469.                             alt1->altnum, sub);
  470.             if ( alt1->predicate!=NULL )
  471.             {
  472.                 fprintf(stderr, " upon ");
  473.                 s_fprT(stderr, non_covered);
  474.             }
  475.             fprintf(stderr, "\n");
  476.         }
  477.         set_free( non_covered );
  478.         non_covered = set_dif(delta, covered_set(alt2->predicate));
  479.         if ( set_deg(non_covered)>0 && WarningLevel>1 )
  480.         {
  481.             fprintf(stderr, ErrHdr, FileStr[alt2->file], alt2->line);
  482.             fprintf(stderr, " warning: alt %d %shas no predicate to resolve ambiguity",
  483.                             alt2->altnum, sub);
  484.             if ( alt2->predicate!=NULL )
  485.             {
  486.                 fprintf(stderr, " upon ");
  487.                 s_fprT(stderr, non_covered);
  488.             }
  489.             fprintf(stderr, "\n");
  490.         }
  491.         set_free( non_covered );
  492.         set_free( delta );
  493.     }
  494.     else fatal_internal("productions have no lookahead in predicate checking routine");
  495. }
  496.  
  497. void
  498. #ifdef __STDC__
  499. HandleAmbiguity( Junction *block, Junction *alt1, Junction *alt2, int jtype )
  500. #else
  501. HandleAmbiguity( block, alt1, alt2, jtype )
  502. Junction *block;
  503. Junction *alt1;
  504. Junction *alt2;
  505. int jtype;
  506. #endif
  507. {
  508.     unsigned **ftbl;
  509.     set *fset, b;
  510.     int i, numAmbig, n, n2;
  511.     Tree *ambig=NULL, *t, *u;
  512.     char *sub = "";
  513.     require(block!=NULL, "NULL block");
  514.     require(block->ntype==nJunction, "invalid block");
  515.  
  516.     /* These sets are used to constrain LL_k set, but are made CLL_k long anyway */
  517.     fset = (set *) calloc(CLL_k+1, sizeof(set));
  518.     require(fset!=NULL, "cannot allocate fset");
  519.     ftbl = (unsigned **) calloc(CLL_k+1, sizeof(unsigned *));
  520.     require(ftbl!=NULL, "cannot allocate ftbl");
  521.     /* create constraint table and count number of possible ambiguities (use<=LL_k) */
  522.     for (n=1,i=1; i<=CLL_k; i++)
  523.     {
  524.         b = set_and(alt1->fset[i], alt2->fset[i]);
  525.         n *= set_deg(b);
  526.         fset[i] = set_dup(b);
  527.         ftbl[i] = set_pdq(b);
  528.         set_free(b);
  529.     }
  530.  
  531.     switch ( jtype )
  532.     {
  533.         case aSubBlk: sub = "of (..) "; break;
  534.         case aOptBlk: sub = "of {..} "; break;
  535.         case aLoopBegin: sub = "of (..)* "; break;
  536.         case aLoopBlk: sub = "of (..)* "; break;
  537.         case aPlusBlk: sub = "of (..)+ "; break;
  538.         case RuleBlk: sub = "of the rule itself "; break;
  539.         default : sub = ""; break;
  540.     }
  541.  
  542.     /* If the block is marked as a compressed lookahead only block, then
  543.      * simply return; ambiguity warning is given only at warning level 2.
  544.      */
  545.     if ( block->approx>0 )
  546.     {
  547.         if ( ParseWithPredicates )
  548.         {
  549.             alt1->predicate = find_predicates((Node *)alt1->p1);
  550.             alt2->predicate = find_predicates((Node *)alt2->p1);
  551.             if ( HoistPredicateContext && (alt1->predicate!=NULL||alt2->predicate!=NULL) )
  552.             ensure_predicates_cover_ambiguous_lookahead_sequences(alt1, alt2, sub, ambig);
  553.         }
  554.  
  555.         if ( WarningLevel>1 )
  556.         {
  557.             fprintf(stderr, ErrHdr, FileStr[alt1->file], alt1->line);
  558.             if ( jtype == aLoopBegin || jtype == aPlusBlk )
  559.                 fprintf(stderr, " warning: optional/exit path and alt(s) %sambiguous upon", sub);
  560.             else
  561.                 fprintf(stderr, " warning(approx): alts %d and %d %sambiguous upon",
  562.                         alt1->altnum, alt2->altnum, sub);
  563.             dumpAmbigMsg(fset, stderr, 0);
  564.         }
  565.         for (i=1; i<=CLL_k; i++) set_free( fset[i] );
  566.         free((char *)fset);
  567.         for (i=1; i<=CLL_k; i++) free( (char *)ftbl[i] );
  568.         free((char *)ftbl);
  569.         return;
  570.     }
  571.  
  572.     /* if all sets have degree 1 for k<LL_k, then must be ambig upon >=1 permutation;
  573.      * don't bother doing full LL(k) analysis.
  574.      * (This "if" block handles the LL(1) case)
  575.      */
  576.     n2 = 0;
  577.     for (i=1; i<LL_k; i++) n2 += set_deg(alt1->fset[i])+set_deg(alt2->fset[i]);
  578.     if ( n2==2*(LL_k-1) )
  579.     {
  580. /* TJP: added to fix the case where LL(1) and syntactic predicates didn't
  581.  * work.  It now recognizes syntactic predicates, but does not like combo:
  582.  * LL(1)/syn/sem predicates. (10/24/93) 
  583.  */
  584.         if ( first_item_is_guess_block((Junction *)alt1->p1)!=NULL )
  585.         {
  586.             if ( WarningLevel==1 )
  587.             {
  588.                 for (i=1; i<=CLL_k; i++) set_free( fset[i] );
  589.                 free((char *)fset);
  590.                 for (i=1; i<=CLL_k; i++) free( (char *)ftbl[i] );
  591.                 free((char *)ftbl);
  592.                 return;
  593.             }
  594.  
  595.             fprintf(stderr, ErrHdr, FileStr[alt1->file], alt1->line);
  596.             if ( jtype == aLoopBegin || jtype == aPlusBlk )
  597.                fprintf(stderr, " warning: optional/exit path and alt(s) %sambiguous upon", sub);
  598.             else
  599.                fprintf(stderr, " warning: alts %d and %d %sambiguous upon",
  600.                        alt1->altnum, alt2->altnum, sub);
  601.             dumpAmbigMsg(fset, stderr, 0);
  602.         }
  603.  
  604.         ambig = NULL;
  605.         if ( LL_k>1 ) ambig = make_tree_from_sets(alt1->fset, alt2->fset);
  606.         if ( ParseWithPredicates )
  607.         {
  608.            /* NOTE: find_predicates() doesn't know that full LL(k) analysis is
  609.             *         not necessary.
  610.             */
  611.            alt1->predicate = find_predicates((Node *)alt1->p1);
  612.            alt2->predicate = find_predicates((Node *)alt2->p1);
  613.            if (HoistPredicateContext&&(alt1->predicate!=NULL||alt2->predicate!=NULL))
  614.               ensure_predicates_cover_ambiguous_lookahead_sequences(alt1, alt2, sub, ambig);
  615.            if ( WarningLevel == 1 &&
  616.                (alt1->predicate!=NULL||alt2->predicate!=NULL))
  617.            {
  618.               for (i=1; i<=CLL_k; i++) set_free( fset[i] );
  619.               free((char *)fset);
  620.               for (i=1; i<=CLL_k; i++) free( (char *)ftbl[i] );
  621.               free((char *)ftbl);
  622.               Tfree(ambig);
  623.               return;
  624.            }
  625.         }
  626. /* end TJP (10/24/93) */
  627.  
  628.         fprintf(stderr, ErrHdr, FileStr[alt1->file], alt1->line);
  629.         if ( jtype == aLoopBegin || jtype == aPlusBlk )
  630.             fprintf(stderr, " warning: optional/exit path and alt(s) %sambiguous upon", sub);
  631.         else
  632.            fprintf(stderr, " warning: alts %d and %d %sambiguous upon",
  633.                    alt1->altnum, alt2->altnum, sub);
  634.         if ( elevel == 3 && LL_k>1 )
  635.         {
  636.            preorder(ambig);
  637.            fprintf(stderr, "\n");
  638.            Tfree(ambig);
  639.            return;
  640.         }
  641.         Tfree(ambig);
  642.         dumpAmbigMsg(fset, stderr, 0);
  643.  
  644.         for (i=1; i<=CLL_k; i++) set_free( fset[i] );
  645.         free((char *)fset);
  646.         for (i=1; i<=CLL_k; i++) free( (char *)ftbl[i] );
  647.         free((char *)ftbl);
  648.         return;
  649.     }
  650.  
  651.     /* in case tree construction runs out of memory, set info to make good err msg */
  652.     CurAmbigAlt1 = alt1->altnum;
  653.     CurAmbigAlt2 = alt2->altnum;
  654.     CurAmbigbtype = sub;
  655.     CurAmbigfile = alt1->file;
  656.     CurAmbigline = alt1->line;
  657.     
  658.     /* Don't do full LL(n) analysis if (...)? block because the block,
  659.        by definition, defies LL(n) analysis.
  660.        If guess (...)? block and ambiguous then don't remove anything from
  661.        2nd alt to resolve ambig.
  662.        Want to predict with LL sup 1 ( n ) decision not LL(n) if guess block
  663.        since it is much cheaper than LL(n).  LL sup 1 ( n ) "covers" the LL(n)
  664.        lookahead information.
  665.  
  666.        Note: LL(n) context cannot be computed for semantic predicates when
  667.        followed by (..)?.
  668.  
  669.        If (..)? then we scream "AAAHHHH!  No LL(n) analysis will help"
  670.  
  671.        Is 'ambig' always defined if we enter this if?  I hope so
  672.        because the 'ensure...()' func references it. TJP Nov 1993.
  673.        */
  674.     if ( first_item_is_guess_block((Junction *)alt1->p1)!=NULL )
  675.     {
  676.         if ( ParseWithPredicates )
  677.         {
  678.             alt1->predicate = find_predicates((Node *)alt1->p1);
  679.             alt2->predicate = find_predicates((Node *)alt2->p1);
  680.             if ( HoistPredicateContext && (alt1->predicate!=NULL||alt2->predicate!=NULL) )
  681.                 ensure_predicates_cover_ambiguous_lookahead_sequences(alt1, alt2, sub, ambig);
  682.             if ( WarningLevel==1 &&
  683.                 (alt1->predicate!=NULL||alt2->predicate!=NULL))
  684.             {
  685.                 for (i=1; i<=CLL_k; i++) set_free( fset[i] );
  686.                 free((char *)fset);
  687.                 for (i=1; i<=CLL_k; i++) free( (char *)ftbl[i] );
  688.                 free((char *)ftbl);
  689.                 return;
  690.             }
  691.         }
  692.  
  693.         if ( WarningLevel>1 )
  694.         {
  695.             fprintf(stderr, ErrHdr, FileStr[alt1->file], alt1->line);
  696.             if ( jtype == aLoopBegin || jtype == aPlusBlk )
  697.                 fprintf(stderr, " warning: optional/exit path and alt(s) %sambiguous upon", sub);
  698.             else
  699.                 fprintf(stderr, " warning: alts %d and %d %sambiguous upon",
  700.                         alt1->altnum, alt2->altnum, sub);
  701.             dumpAmbigMsg(fset, stderr, 0);
  702.         }
  703.  
  704.         for (i=1; i<=CLL_k; i++) set_free( fset[i] );
  705.         free((char *)fset);
  706.         for (i=1; i<=CLL_k; i++) free( (char *)ftbl[i] );
  707.         free((char *)ftbl);
  708.         return;
  709.     }
  710.     
  711.     /* Not resolved with (..)? block.  Do full LL(n) analysis */
  712.     
  713.     /* ambig is the set of k-tuples truly in common between alt 1 and alt 2 */
  714.     ambig = VerifyAmbig(alt1, alt2, ftbl, fset, &t, &u, &numAmbig);
  715.     for (i=1; i<=CLL_k; i++) free( (char *)ftbl[i] );
  716.     free((char *)ftbl);
  717.  
  718.     /* are all things in intersection really ambigs? */
  719.     if ( numAmbig < n )
  720.     {
  721.         Tree *v;
  722.  
  723.         /* remove ambig permutation from 2nd alternative to resolve ambig;
  724.          * We want to compute the set of artificial tuples, arising from
  725.          * LL sup 1 (n) compression, that collide with real tuples from the
  726.          * 2nd alternative.  This is the set of "special case" tuples that
  727.          * the LL sup 1 (n) decision template maps incorrectly.
  728.          */
  729.         if ( ambig!=NULL )
  730.         {
  731.             for (v=ambig->down; v!=NULL; v=v->right)
  732.             {
  733.                 u = trm_perm(u, v);
  734.             }
  735. /*            fprintf(stderr, "after rm alt2:"); preorder(u); fprintf(stderr, "\n");*/
  736.         }
  737.         Tfree( t );
  738.         alt1->ftree = tappend(alt1->ftree, u);
  739.         alt1->ftree = tleft_factor(alt1->ftree);
  740.     }
  741.  
  742.     if ( ambig==NULL )
  743.     {
  744.         for (i=1; i<=CLL_k; i++) set_free( fset[i] );
  745.         free((char *)fset);
  746.         return;
  747.     }
  748.  
  749.     ambig = tleft_factor(ambig);
  750.  
  751. /* TJP:
  752.  * At this point, we surely have an LL(k) ambiguity.  Check for predicates
  753.  */
  754.     if ( ParseWithPredicates )
  755.     {
  756.         alt1->predicate = find_predicates((Node *)alt1->p1);
  757.         alt2->predicate = find_predicates((Node *)alt2->p1);
  758.         if ( HoistPredicateContext && (alt1->predicate!=NULL||alt2->predicate!=NULL) )
  759.            ensure_predicates_cover_ambiguous_lookahead_sequences(alt1, alt2, sub, ambig);
  760.         if ( WarningLevel==1 &&
  761.              (alt1->predicate!=NULL||alt2->predicate!=NULL))
  762.         {
  763.             /* We found at least one pred for at least one of the alts;
  764.              * If warnings are low, just return.
  765.              */
  766.             Tfree(ambig);
  767.             return;
  768.         }
  769.         /* else we're gonna give a warning */
  770.     }
  771. /* end TJP addition */
  772.  
  773.     fprintf(stderr, ErrHdr, FileStr[alt1->file], alt1->line);
  774.     if ( jtype == aLoopBegin || jtype == aPlusBlk )
  775.         fprintf(stderr, " warning: optional/exit path and alt(s) %sambiguous upon", sub);
  776.     else
  777.         fprintf(stderr, " warning: alts %d and %d %sambiguous upon",
  778.                     alt1->altnum, alt2->altnum, sub);
  779.     if ( elevel == 3 )
  780.     {
  781.         preorder(ambig->down);
  782.         fprintf(stderr, "\n");
  783.         Tfree(ambig);
  784.         return;
  785.     }
  786.     Tfree(ambig);
  787.     dumpAmbigMsg(fset, stderr, 0);
  788.     for (i=1; i<=CLL_k; i++) set_free( fset[i] );
  789.     free((char *)fset);
  790. }
  791.  
  792. /* Don't analyze alpha block of (alpha)?beta; if (alpha)? then analyze
  793.  * Return the 1st node of the beta block if present else return j.
  794.  */
  795. Junction *
  796. #ifdef __STDC__
  797. analysis_point( Junction *j )
  798. #else
  799. analysis_point( j )
  800. Junction *j;
  801. #endif
  802. {
  803.     Junction *gblock;
  804.  
  805.     if ( j->ntype!=nJunction ) return j;
  806.     gblock = first_item_is_guess_block((Junction *)j);
  807.  
  808.     if ( gblock!=NULL )
  809.     {
  810.         Junction *past = gblock->end;
  811.         Junction *p;
  812.         require(past!=NULL, "analysis_point: no end block on (...)? block");
  813.  
  814.         for (p=(Junction *)past->p1; p!=NULL; )
  815.         {
  816.             if ( p->ntype==nAction )
  817.             {
  818.                 p=(Junction *)((ActionNode *)p)->next;
  819.                 continue;
  820.             }
  821.             if ( p->ntype!=nJunction )
  822.             {
  823.                 return (Junction *)past->p1;
  824.             }
  825.             if ( p->jtype==EndBlk || p->jtype==EndRule )
  826.             {
  827.                 return j;
  828.             }
  829.             p=(Junction *)p->p1;
  830.         }
  831.     }
  832.     return j;
  833. }
  834.  
  835. set
  836. #ifdef __STDC__
  837. First( Junction *j, int k, int jtype, int *max_k )
  838. #else
  839. First( j, k, jtype, max_k )
  840. Junction *j;
  841. int k;
  842. int jtype;
  843. int *max_k;
  844. #endif
  845. {
  846.     Junction *alt1, *alt2;
  847.     set a, rk, fCurBlk;
  848.     int savek;
  849.     int p1, p2;
  850.     require(j->ntype==nJunction, "First: non junction passed");
  851.  
  852.     /* C o m p u t e  F I R S T  s e t  w i t h  k  l o o k a h e a d */
  853.     fCurBlk = rk = empty;
  854.     for (alt1=j; alt1!=NULL; alt1 = (Junction *)alt1->p2)
  855.     {
  856.         Junction *p = analysis_point((Junction *)alt1->p1);
  857.         REACH(p, k, &rk, alt1->fset[k]);
  858.         require(set_nil(rk), "rk != nil");
  859.         set_free(rk);
  860.         set_orin(&fCurBlk, alt1->fset[k]);
  861.     }
  862.  
  863.     /* D e t e c t  A m b i g u i t i e s */
  864.     *max_k = 1;
  865.     for (p1=1,alt1=j; alt1!=NULL; alt1 = (Junction *)alt1->p2, p1++)
  866.     {
  867.         for (p2=1,alt2=(Junction *)alt1->p2; alt2!=NULL; alt2 = (Junction *)alt2->p2, p2++)
  868.         {
  869.             savek = k;
  870.             a = set_and(alt1->fset[k], alt2->fset[k]);
  871.             while ( !set_nil(a) )
  872.             {
  873.                 /* if we have hit the max k requested, just give warning */
  874.                 if ( j->approx==k ) {
  875.                 }
  876.  
  877.                 if ( k==CLL_k )
  878.                 {
  879. #ifdef NOT_USED
  880.                     int save_LL_k = LL_k;
  881.                     int save_CLL_k = CLL_k;
  882.                     /* Get new LL_k from interactive feature if enabled */
  883.                     if ( AImode )
  884.                         AmbiguityDialog(j, jtype, alt1, alt2, &CLL_k, &LL_k);
  885. #endif
  886.                     *max_k = CLL_k;
  887.                     HandleAmbiguity(j, alt1, alt2, jtype);
  888.                     break;
  889.                 }
  890.                 else
  891.                 {
  892.                     Junction *p = analysis_point((Junction *)alt1->p1);
  893.                     Junction *q = analysis_point((Junction *)alt2->p1);
  894.                     k++;    /* attempt ambig alts again with more lookahead */
  895.                     REACH(p, k, &rk, alt1->fset[k]);
  896.                     require(set_nil(rk), "rk != nil");
  897.                     REACH(q, k, &rk, alt2->fset[k]);
  898.                     require(set_nil(rk), "rk != nil");
  899.                     set_free(a);
  900.                     a = set_and(alt1->fset[k], alt2->fset[k]);
  901.                     if ( k > *max_k ) *max_k = k;
  902.                 }
  903.             }
  904.             set_free(a);
  905.             k = savek;
  906.         }
  907.     }
  908.  
  909.     return fCurBlk;
  910. }
  911.